﻿using HandyControl.Controls;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace 刷屏测试软件.View
{
    /// <summary>
    /// TestView.xaml 的交互逻辑
    /// </summary>
    public partial class TestView : UserControl
    {
        ViewModel.TestViewModel viewModel = new ViewModel.TestViewModel();
        BLL.TestViewBLL testViewBLL = null;
        BtLabelDAL.HttpDAL httpDAL = new BtLabelDAL.HttpDAL();
        public static bool selecting = false;
        public TestView()
        {
            try
            {
                //设置为中文
                HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-cn");
                InitializeComponent();
                this.DataContext = viewModel;
                testViewBLL = new BLL.TestViewBLL(List, viewModel);
            }
            catch (System.Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void PackIcon_MouseMove(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void PackIcon_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void ButtonLightLed_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TextBoxMac.IsEnabled = false;
                int id = int.Parse((sender as Button).Content.ToString());
                var item = viewModel.MacItems.ToList().Where(it => it.id == id).First();
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        testViewBLL.LightLed(item);
                        Dispatcher.Invoke(() =>
                        {
                            TextBoxMac.IsEnabled = true;
                            TextBoxMac.Clear();
                            TextBoxMac.Focus();
                            TextBoxMac.SelectAll();
                        });
                    }
                    catch (System.Exception ex)
                    {
                        Growl.Warning(ex.Message);
                    }
                });
            }
            catch (System.Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void ButtonAllLightLed_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //foreach (var item in viewModel.MacItems)
                //{
                //    Task.Factory.StartNew(() =>
                //    {
                //        try
                //        {
                //            testViewBLL.LightLed(item);
                //            Thread.Sleep(500);
                //        }
                //        catch (System.Exception ex)
                //        {
                //            Growl.Warning(ex.Message);
                //        }
                //    });
                //}
                TextBoxMac.IsEnabled = false;
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        testViewBLL.LightLeds(viewModel.MacItems.ToList());
                        Dispatcher.Invoke(() =>
                        {
                            TextBoxMac.IsEnabled = true;
                            TextBoxMac.Clear();
                            TextBoxMac.Focus();
                            TextBoxMac.SelectAll();
                        });
                    }
                    catch (System.Exception ex)
                    {
                        Growl.Warning(ex.Message);
                    }
                });
            }
            catch (System.Exception ex)
            {
                Growl.Warning(ex.Message);
            }
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (var item in viewModel.MacItems)
                {
                    if (RadioButtonAll.IsChecked == true && item.ItemHeight == 0)
                    {
                        selecting = false;
                        item.ItemHeight = 40;
                    }
                    if (RadioButtonWait.IsChecked == true)
                    {
                        selecting = true;
                        if (item.BackColor == Brushes.LightGray || item.BackColor == Brushes.Gold || item.BackColor == Brushes.Tomato)
                        {
                            item.ItemHeight = 40;
                        }
                        else
                        {
                            item.ItemHeight = 0;
                        }
                    }
                    if (RadioButtonPass.IsChecked == true)
                    {
                        selecting = false;
                        item.ItemHeight = item.BackColor == Brushes.YellowGreen ? 40 : 0;
                    }
                }

            }
            catch (System.Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (TextBoxMac.Text.Length != 12)
                    {
                        TextBoxMac.Focus();
                        TextBoxMac.SelectAll();
                        Growl.Warning("扫码长度错误!");
                        return;
                    }
                    string mac = TextBoxMac.Text.Trim().ToUpper();
                    mac = mac.Substring(0, 2) + " " + mac.Substring(2, 2) + " " +
                        mac.Substring(4, 2) + " " + mac.Substring(6, 2) + " " +
                        mac.Substring(8, 2) + " " + mac.Substring(10, 2);
                    ViewModel.TestViewModel.Item item = null;
                    var items = viewModel.MacItems.Where(it => it.Mac == mac).ToList();
                    if (items.Count() != 0)
                    {
                        if (items[0].time.AddSeconds(5) > DateTime.Now)
                        {
                            Growl.Warning("已扫码");
                            TextBoxMac.Clear();
                            TextBoxMac.Focus();
                            TextBoxMac.SelectAll();
                            return;
                        }
                        items[0].time = DateTime.Now;
                        items[0].RefreshResult = "等待刷屏";
                        items[0].LightResult = "等待点灯";
                        items[0].BackColor = Brushes.LightGray;
                        item = items[0];
                        goto label1;
                    }
                    else
                    {
                        item = new ViewModel.TestViewModel.Item()
                        {
                            id = viewModel.MacItems.Count,
                            IsCheck = false,
                            Mac = mac,
                            RefreshResult = "等待刷屏",
                            LightResult = "等待点灯",
                            BackColor = Brushes.LightGray,
                        };
                        var labelInfo = httpDAL.GetLabelInfo(item.Mac);
                        item.Ver = labelInfo.dataLoad.ver;
                        item.Battery = labelInfo.dataLoad.battery;
                        item.dBm = labelInfo.dataLoad.slevel;
                        viewModel.MacItems.Add(item);

                    }
                label1:
                    InitCount();
                    if (RadioButtonWait.IsChecked != true && RadioButtonAll.IsChecked != true)
                    {
                        item.ItemHeight = 0;
                    }
                    else
                    {
                        item.ItemHeight = 40;
                    }
                    List.ScrollIntoView(List.Items[item.id]);
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            testViewBLL.RefreshPic(item);
                        }
                        catch (System.Exception ex)
                        {
                            Growl.Warning(ex.Message);
                        }
                    });
                    TextBoxMac.Clear();
                    TextBoxMac.Focus();
                    TextBoxMac.SelectAll();
                }
            }
            catch (System.Exception ex)
            {
                InitCount();
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var item in viewModel.MacItems)
            {
                item.IsCheck = true;
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (var item in viewModel.MacItems)
            {
                item.IsCheck = false;
            }
        }
        private void InitCount()
        {
            int wait = 0;
            int ing = 0;
            int pass = 0;
            int fail = 0;
            foreach (var item in viewModel.MacItems)
            {
                if (item.BackColor == Brushes.LightGray)
                {
                    wait++;
                }
                if (item.BackColor == Brushes.Gold)
                {
                    ing++;
                }
                if (item.BackColor == Brushes.YellowGreen)
                {
                    pass++;
                }
                if (item.BackColor == Brushes.Tomato)
                {
                    fail++;
                }
            }
            viewModel.AllCount = wait + ing + pass + fail;
            viewModel.WaitCount = wait;
            viewModel.IngCount = ing;
            viewModel.PassCount = pass;
            viewModel.FailCount = fail;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            viewModel.MacItems.Clear();
            viewModel.AllCount = 0;
            viewModel.WaitCount = 0;
            viewModel.IngCount = 0;
            viewModel.PassCount = 0;
            viewModel.FailCount = 0;
        }
    }
}
